//
//  SSSWaitDiscountViewController.m
//  SSSMerchant
//
//  Created by Darker on 2017/3/27.
//  Copyright © 2017年 xnxin. All rights reserved.
//

#import "SSSWaitDiscountViewController.h"
#import "SSSWaitDiscopuntCell.h"
#import "SSSOnlinePayViewController.h"
#import "XHRequestManager.h"
#import "NSArray+XHAdd.h"
#import "XHTableViewNetworkHelper.h"
#import "APITool.h"
#import "XHUIHelper.h"
#import "NSDictionaryAdditions.h"
#import "XHRequestManager.h"
@interface SSSWaitDiscountViewController ()<UITableViewDelegate,UITableViewDataSource,SSSWaitDiscountDelegate>

{
    UITableView *_waitDiscountTableView;
    UILabel *_allMoneyNuLabel;//订单金额
    UILabel *_oderNuLabel;//订单数量
    UIView *_headerView;
    float priceNumber;
    int discountNumber;
    UIButton *deleteBtn;
    NSString *moneyStr;
    NSInteger _currentPage;//当前页数
    NSString *_dicountStr;
    NSString *_allMoneyStr;
    NSMutableArray *_idArray;//订单id数组;
    BOOL isFirst;
    NSMutableArray * _waitDataArray;
}

/** _waitDiscountTableView */
@property (nonatomic, strong) UITableView *waitDiscountTableView;
/** 下方选择selectedBtn */
@property (nonatomic, strong) UIButton *selectedBtn;
@property (nonatomic, strong) XHTableViewNetworkHelper *networkHelper;

@end

static NSString *WaitDiscopuntID    = @"WaitDiscopuntID";

@implementation SSSWaitDiscountViewController

//- (void)loadView {
//    [super loadView];
//    _networkHelper = [[XHTableViewNetworkHelper alloc] initWithURL:[APITool urlStr:@"/bApi/order/pendingList"]];
//}

//- (void)viewWillDisappear:(BOOL)animated{
//    
//    [super viewWillDisappear:animated];
//    
//    [_waitDiscountTableView.mj_footer endRefreshing];
//    [_waitDiscountTableView.mj_header endRefreshing];
//}
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.view.backgroundColor = [UIColor colorWithHex:@"F9F9FA"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    isFirst = YES;
    
    _idArray = [[NSMutableArray alloc]init];
    _waitDataArray = [[NSMutableArray alloc]init];
    
    [self setUpApperance];
    priceNumber = 0.00;
    discountNumber = 0;

    [self refresh];
    
//    //模拟数据
//    for (int i = 0; i < 10; i++) {
//
//        NSMutableDictionary *dataDic = [[NSMutableDictionary alloc]init];
//        NSString *timeStr = [NSString stringWithFormat:@"2017-19-%d",i];
//        NSString *moneyStr = [NSString stringWithFormat:@"%.1f",199.0+i];
//        NSString *discountStr = [NSString stringWithFormat:@"%.0d",80+i];
//        [dataDic setValue:timeStr forKey:@"date"];
//        [dataDic setValue:moneyStr forKey:@"goodsPrice"];
//        [dataDic setValue:discountStr forKey:@"discountNu"];
//        SSSWaitModel *model = [SSSWaitModel modelWithJSON:dataDic];
//        [_waitDataArray addObject:model];
//        [_waitDiscountTableView reloadData];
//    }
//    [_waitDiscountTableView reloadData];
    
    
}

- (void)refresh {

    ZDRefreshHeader *header = [ZDRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 马上进入刷新状态
    [header beginRefreshing];
    // 设置header
    _waitDiscountTableView.mj_header = header;
    
    // 上拉加载
    _waitDiscountTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
//    [self.networkHelper refreshDataSuccess:^(id responseObj) {
//
//        [_waitDataArray removeAllObjects];
//        [self processData:responseObj];
//    } fail:^(NSError *error) {
//
//        [XHUIHelper showError:error withView:self.view];
//    }];
}
- (void)loadNewData{
    
    _selectedBtn.selected = NO;
    [self.selectedBtn setImage:[UIImage imageNamed:@"icon_unchecked"] forState:UIControlStateNormal];
    
    [_waitDiscountTableView.mj_footer endRefreshing];
    _currentPage = 1;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"currentPage"] = @(_currentPage);
    params[@"pageSize"] = @"20";
    
    //NSString *str = [NSString stringWithFormat:@"http://192.168.0.230:8080/ssstc/bApi/order/pendingList"];
    
    [[XHRequestManager manager] getRequest:[APITool urlStr:kOrderPending] params:params success:^(id  _Nonnull responseObj) {
        
        XHLog(@"-----%@",responseObj);
        if ([responseObj[@"status"] intValue] == 0){
            
            NSArray *array = [SSSWaitModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"][@"page"][@"pageList"]];

            [_waitDataArray removeAllObjects];
            _waitDataArray = [NSMutableArray array];
            [_waitDataArray addObjectsFromArray:array];
            
            NSDictionary *dict = @{@"allMoney":responseObj[@"data"][@"totalMoney"],@"rangli":responseObj[@"data"][@"orderNumber"]};
            
            //发送通知传值
            NSNotification *notification =[NSNotification notificationWithName:@"sendWaitingNuStr" object:nil userInfo:dict];
            [[NSNotificationCenter defaultCenter] postNotification:notification];
            
            //回到主线程刷新表格
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //刷新表格
                [_waitDiscountTableView reloadData];
                
            });
            
            [_waitDiscountTableView.mj_header endRefreshing];
            
            
            if ([responseObj[@"data"][@"page"][@"hasNextPage"] intValue] == 0) {
                
                [_waitDiscountTableView.mj_footer endRefreshingWithNoMoreData];
                
            }else{
                
                [_waitDiscountTableView.mj_header endRefreshing];
            }
//            if (isFirst) {
//                
//                _waitDiscountTableView.mj_footer.hidden = YES;
//            }
//            isFirst = NO;
            
        }else{
            
            //showTipHudStr(kResponseMessage);
            [_waitDiscountTableView.mj_header endRefreshing];
        }

        
    } failure:^(NSError * _Nonnull error) {
        
        XHLog(@"---error----%@",error.localizedDescription);
                showTipHudStr(error.localizedDescription);
                 [_waitDiscountTableView.mj_header endRefreshing];
        
    }];
    
    
//    [ZDNetManager zd_requestWithType:ZDHttpRequestTypeGet UrlString:[APITool urlStr:@"bApi/order/pendingList"] Parameters:params SuccessBlock:^(id response) {
//        
//        XHLog(@"-----%@",response);
//        if ([response[@"status"] intValue] == 0){
//            
//            if (_waitDataArray.count == 0) {
//                
//                self.selectedBtn.enabled = NO;
//                showTipHudStr(@"数据暂未加载，暂不能点击");
//            }
//            
//            _waitDataArray = [SSSWaitModel mj_objectArrayWithKeyValuesArray:response[@"data"][@"page"][@"pageList"]];
//            
//            NSDictionary *dict = @{@"allMoney":response[@"data"][@"totalMoney"],@"rangli":response[@"data"][@"orderNumber"]};
//            
//            //发送通知传值
//            NSNotification *notification =[NSNotification notificationWithName:@"sendWaitingNuStr" object:nil userInfo:dict];
//            [[NSNotificationCenter defaultCenter] postNotification:notification];
//            
//            //回到主线程刷新表格
//            dispatch_async(dispatch_get_main_queue(), ^{
//                
//                //刷新表格
//                [_waitDiscountTableView reloadData];
//                
//            });
//            
//            [_waitDiscountTableView.mj_header endRefreshing];
//            
//            
//            if ([response[@"data"][@"page"][@"hasNextPage"] intValue] == 0) {
//                
//                [_waitDiscountTableView.mj_footer endRefreshingWithNoMoreData];
//                
//            }else{
//                
//                [_waitDiscountTableView.mj_header endRefreshing];
//            }
//            if (isFirst) {
//                
//                _waitDiscountTableView.mj_footer.hidden = YES;
//            }
//            isFirst = NO;
//            
//        }else{
//            
//            showTipHudStr(kResponseMessage);
//            [_waitDiscountTableView.mj_header endRefreshing];
//        }
//
//    } FailureBlock:^(NSError *error) {
//        
//        XHLog(@"---error----%@",error.localizedDescription);
//        showTipHudStr(error.localizedDescription);
//         [_waitDiscountTableView.mj_header endRefreshing];
//        
//    } progress:^(int64_t bytesProgress, int64_t totalBytesProgress) {
//        
//    }];
//
    
}
- (void)loadMoreData{
    _selectedBtn.selected = NO;
    [self.selectedBtn setImage:[UIImage imageNamed:@"icon_unchecked"] forState:UIControlStateNormal];
    
    [_waitDiscountTableView.mj_header endRefreshing];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"currentPage"] = @(++_currentPage);
    params[@"pageSize"] = @"20";
    
    [[XHRequestManager manager] getRequest:kConfigPath(kFindTradeCode) params:params success:^(id  _Nonnull responseObj) {
        
        
        XHLog(@"-----%@",responseObj);
                if ([responseObj[@"status"] intValue] == 0){
        
                    NSArray *dArray = [SSSWaitModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"][@"page"][@"pageList"]];
        
                    [_waitDataArray addObjectsFromArray:dArray];
                    
                    //回到主线程刷新表格
                    dispatch_async(dispatch_get_main_queue(), ^{
        
                        //刷新表格
                        [_waitDiscountTableView reloadData];
        
                    });
        
                    _waitDiscountTableView.mj_footer.hidden = (_currentPage == 0);
        
                    if ([responseObj[@"data"][@"page"][@"hasNextPage"] intValue] == 0) {
        
                        [_waitDiscountTableView.mj_footer endRefreshingWithNoMoreData];
        
                    }else{
        
                        [_waitDiscountTableView.mj_header endRefreshing];
                    }
                    
                }else{
                    
                    showTipHudStr(responseObj[@"message"]);
                    [_waitDiscountTableView.mj_header endRefreshing];
                }

        
    } failure:^(NSError * _Nonnull error) {
        
        [QMUITips showError:error.localizedDescription inView:self.view hideAfterDelay:2];
    }];

}

//- (void)loadMore {
//    [self.networkHelper loadMoreDataSuccess:^(id responseObj) {
//        [self processData:responseObj];
//    } fail:^(NSError *error) {
//        [XHUIHelper showError:error withView:self.view];
//    }];
//}

///fetch data from internet
//- (void)processData:(NSDictionary*)data {
//
//    NSArray *modelArray = [NSArray modelArrayWithClass:[SSSWaitModel class]
//                                                  json:[[data objectOrNilForKey:@"page"] objectOrNilForKey:@"pageList"]];
//    [_waitDataArray appendObjects:modelArray];
//    [_waitDiscountTableView reloadData];
//}

- (void)setUpApperance{
    
    _headerView = [[UIView alloc]init];
    _headerView.backgroundColor = [UIColor colorWithHex:@"F9F9FA"];
    _headerView.frame = CGRectMake(0, 0, KSCREEN_Width, HeigthIos6CompeteWithCurrent(57));
    [self.view addSubview:_headerView];
    
    UILabel *selectLabel = [UILabel new];
    selectLabel.text = @"选择";
    selectLabel.font = [SSSFont systemFontOfSize:14];
    selectLabel.textColor = [UIColor colorWithHex:@"666666"];
    [_headerView addSubview:selectLabel];
    selectLabel.sd_layout
    .leftSpaceToView(_headerView,WidthIos6CompeteWithCurrent(20))
    .centerYEqualToView(_headerView)
    .heightIs(HeigthIos6CompeteWithCurrent(13))
    .widthIs([ZDKitManager zd_AutoSizeOfWidthWithText:selectLabel.text font:selectLabel.font height:HeigthIos6CompeteWithCurrent(13)]);
    
    
    UILabel *dateLabel = [UILabel new];
    dateLabel.text = @"日期";
    dateLabel.font = [SSSFont systemFontOfSize:14];
    dateLabel.textColor = [UIColor colorWithHex:@"666666"];
    [_headerView addSubview:dateLabel];
    dateLabel.sd_layout
    .leftSpaceToView(_headerView,WidthIos6CompeteWithCurrent(97))
    .centerYEqualToView(_headerView)
    .heightIs(HeigthIos6CompeteWithCurrent(13))
    .widthIs([ZDKitManager zd_AutoSizeOfWidthWithText:dateLabel.text font:dateLabel.font height:HeigthIos6CompeteWithCurrent(13)]);
    
    UILabel *oderNuLabel = [UILabel new];
    oderNuLabel.text = @"订单金额";
    oderNuLabel.font = [SSSFont systemFontOfSize:14];
    oderNuLabel.textColor = [UIColor colorWithHex:@"666666"];
    [_headerView addSubview:oderNuLabel];
    oderNuLabel.sd_layout
    .leftSpaceToView(_headerView,WidthIos6CompeteWithCurrent(196))
    .centerYEqualToView(_headerView)
    .heightIs(HeigthIos6CompeteWithCurrent(13))
    .widthIs([ZDKitManager zd_AutoSizeOfWidthWithText:oderNuLabel.text font:oderNuLabel.font height:HeigthIos6CompeteWithCurrent(13)]);
    
    UILabel *discountNuLabel = [UILabel new];
    discountNuLabel.text = @"让利金额";
    discountNuLabel.font = [SSSFont systemFontOfSize:14];
    discountNuLabel.textColor = [UIColor colorWithHex:@"666666"];
    [_headerView addSubview:discountNuLabel];
    discountNuLabel.sd_layout
    .leftSpaceToView(_headerView,WidthIos6CompeteWithCurrent(296))
    .centerYEqualToView(_headerView)
    .heightIs(HeigthIos6CompeteWithCurrent(13))
    .widthIs([ZDKitManager zd_AutoSizeOfWidthWithText:discountNuLabel.text font:discountNuLabel.font height:HeigthIos6CompeteWithCurrent(13)]);
    
    
    
    //底部视图
    UIView *bottomView = [UIView new];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    bottomView.sd_layout
    .bottomEqualToView(self.view)
    .leftEqualToView(self.view)
    .rightEqualToView(self.view)
    .heightIs(HeigthIos6CompeteWithCurrent(50));
    bottomView.layer.borderColor = [UIColor colorWithHex:@"E5E5E5"].CGColor;
    bottomView.layer.borderWidth = 1;
    
    
    self.selectedBtn = [UIButton new];
    [bottomView addSubview:self.selectedBtn];
    
    [self.selectedBtn setTitle:@" 全选" forState:UIControlStateNormal];
    
    self.selectedBtn.titleLabel.font = [SSSFont systemFontOfSize:14];
    self.selectedBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    // 颜色
    [self.selectedBtn setTitleColor:[UIColor colorWithHex:@"333333"] forState:UIControlStateNormal];
    // 按钮不能被点击时的颜色
    [self.selectedBtn setImage:[UIImage imageNamed:@"icon_unchecked"] forState:UIControlStateNormal];
    [self.selectedBtn setImage:[UIImage imageNamed:@"icon_checked"] forState:UIControlStateSelected];
    
    // 事件
    [self.selectedBtn addTarget:self action:@selector(allSelectedBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.selectedBtn sizeToFit];
    
    
    self.selectedBtn.sd_layout
    .leftSpaceToView(bottomView,WidthIos6CompeteWithCurrent(15))
    .centerYEqualToView(bottomView)
    .heightIs(HeigthIos6CompeteWithCurrent(25))
    .widthIs(WidthIos6CompeteWithCurrent(60));
    
    UIView *contentView = [UIView new];
    contentView.backgroundColor = [UIColor clearColor];
    [bottomView addSubview:contentView];
    contentView.sd_layout
    .leftSpaceToView(self.selectedBtn,WidthIos6CompeteWithCurrent(32))
    .centerYEqualToView(bottomView)
    .heightIs(HeigthIos6CompeteWithCurrent(30))
    .widthIs(WidthIos6CompeteWithCurrent(70));
    
    //共计订单金额
    UILabel *allMoneyLabel = [UILabel new];
    [contentView addSubview:allMoneyLabel];
    allMoneyLabel.text = @"共计订单金额";
    allMoneyLabel.font = [SSSFont systemFontOfSize:11];
    allMoneyLabel.textColor = [UIColor colorWithHex:@"999999"];
    allMoneyLabel.textAlignment = NSTextAlignmentCenter;
    allMoneyLabel.sd_layout
    .leftEqualToView(contentView)
    .rightEqualToView(contentView)
    .bottomEqualToView(contentView)
    .heightIs(HeigthIos6CompeteWithCurrent(11));
    
    //总计金额数
    _allMoneyNuLabel = [UILabel new];
    [contentView addSubview:_allMoneyNuLabel];
    _allMoneyNuLabel.text = @"0.00";
    _allMoneyNuLabel.textColor = [UIColor colorWithHex:@"40B87B"];
    _allMoneyNuLabel.font = [SSSFont systemFontOfSize:17];
    _allMoneyNuLabel.textAlignment = NSTextAlignmentCenter;
    _allMoneyNuLabel.sd_layout
    .leftEqualToView(contentView)
    .topEqualToView(contentView)
    .rightEqualToView(contentView)
    .bottomSpaceToView(allMoneyLabel,HeigthIos6CompeteWithCurrent(5));
    
    //删除点单按钮
    deleteBtn = [UIButton new];
    [bottomView addSubview:deleteBtn];
    deleteBtn.sd_layout
    .rightEqualToView(bottomView)
    .topEqualToView(bottomView)
    .bottomEqualToView(bottomView)
    .widthIs(WidthIos6CompeteWithCurrent(100));
    deleteBtn.titleLabel.font = [SSSFont systemFontOfSize:16];
    [deleteBtn setTitle:@"提交让利" forState:UIControlStateNormal];
    [deleteBtn addTarget:self action:@selector(commitBtn) forControlEvents:UIControlEventTouchDown];
//    [deleteBtn setBackgroundColor:[UIColor colorWithHex:@"40B87B"]];
//    [deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [deleteBtn setBackgroundColor:[UIColor colorWithHex:@"CCCCCC"]];
    [deleteBtn setTitleColor:[UIColor colorWithHex:@"FFFFFF"] forState:UIControlStateDisabled];
    deleteBtn.enabled = NO;

    //订单数量
    UIView *contentView1 = [UIView new];
    contentView1.backgroundColor = [UIColor clearColor];
    [bottomView addSubview:contentView1];
    contentView1.sd_layout
    .rightSpaceToView(deleteBtn,WidthIos6CompeteWithCurrent(29))
    .centerYEqualToView(bottomView)
    .heightIs(HeigthIos6CompeteWithCurrent(30))
    .widthIs(WidthIos6CompeteWithCurrent(50));
    
    //订单数量label
    UILabel * orderLabel = [UILabel new];
    orderLabel.text = @"订单数量";
    orderLabel.font = [SSSFont systemFontOfSize:11];
    orderLabel.textAlignment = NSTextAlignmentCenter;
    orderLabel.textColor = [UIColor colorWithHex:@"999999"];
    [contentView1 addSubview:orderLabel];
    orderLabel.sd_layout
    .leftEqualToView(contentView1)
    .rightEqualToView(contentView1)
    .bottomEqualToView(contentView1)
    .heightIs(HeigthIos6CompeteWithCurrent(11));
    
    _oderNuLabel = [UILabel new];
    [contentView1 addSubview:_oderNuLabel];
    _oderNuLabel.text = @"0";
    _oderNuLabel.textColor = [UIColor colorWithHex:@"40B87B"];
    _oderNuLabel.font = [SSSFont systemFontOfSize:17];
    _oderNuLabel.textAlignment = NSTextAlignmentCenter;
    _oderNuLabel.sd_layout
    .leftEqualToView(contentView1)
    .topEqualToView(contentView1)
    .rightEqualToView(contentView1)
    .bottomSpaceToView(orderLabel,HeigthIos6CompeteWithCurrent(5));
    
    //[_waitDiscountTableView reloadData];
    
    
    _waitDiscountTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, HeigthIos6CompeteWithCurrent(57), KSCREEN_Width, KSCREEN_Height - HeigthIos6CompeteWithCurrent(50) - 64 - HeigthIos6CompeteWithCurrent(59) - HeigthIos6CompeteWithCurrent(116) - HeigthIos6CompeteWithCurrent(57)) style:UITableViewStylePlain];
    _waitDiscountTableView.backgroundColor = [UIColor clearColor];
    _waitDiscountTableView.delegate = self;
    _waitDiscountTableView.dataSource = self;
//    _waitDiscountTableView.tableHeaderView = _headerView;
    _waitDiscountTableView.rowHeight = HeigthIos6CompeteWithCurrent(49);
    _waitDiscountTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_waitDiscountTableView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _waitDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Class currentClass = [SSSWaitDiscopuntCell  class];
    
    SSSWaitDiscopuntCell  *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(currentClass)];
    if (!cell) {
        cell = [[SSSWaitDiscopuntCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:WaitDiscopuntID];
    }
    SSSWaitModel *model = [_waitDataArray objectOrNilAtIndex:indexPath.row];
    cell.delegate = self;
    [cell customDataWithModel:model];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [_waitDiscountTableView deselectRowAtIndexPath:indexPath animated:YES];
    SSSWaitDiscopuntCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    SSSWaitModel *model = _waitDataArray[indexPath.row];

//    if (tableView.indexPathsForSelectedRows.count > 0) {
//
//         [cell.selectedBtn setBackgroundImage:[UIImage imageNamed:@"icon_checked"] forState:UIControlStateNormal];
//        [self numerationPrice];
//        
//    }else{
//
//        [cell.selectedBtn setBackgroundImage:[UIImage imageNamed:@"icon_unchecked"] forState:UIControlStateSelected];
//        [self numerationPrice];
//
//    }
    cell.selectedBtn.selected = !cell.selectedBtn.selected;
    model.isSelected = !model.isSelected;
    
    if (model.isSelected) {
        
        [_idArray addObject:[NSString stringWithFormat:@"%ld",model.ID]];
        [cell.delegate isSelect:cell andModel:model];
         [cell.selectedBtn setBackgroundImage:[UIImage imageNamed:@"icon_checked"] forState:UIControlStateSelected];
         [self.selectedBtn setImage:[UIImage imageNamed:@"icon_checked"] forState:UIControlStateSelected];
        
    }else{
    
        [_idArray removeLastObject];
        [cell.selectedBtn setBackgroundImage:[UIImage imageNamed:@"icon_unchecked"] forState:UIControlStateNormal];
        [self.selectedBtn setImage:[UIImage imageNamed:@"icon_unchecked"] forState:UIControlStateNormal];
        [cell.delegate isSelect:cell andModel:model];
    }
    
    [cell.delegate isSelect:cell andModel:model];
    [_idArray removeAllObjects];
    [_idArray addObject:[NSString stringWithFormat:@"%ld",model.ID]];

}
- (void)isSelect:(UITableViewCell *)cell andModel:(SSSWaitModel *)model
{
    //计算价格
    [self numerationPrice];
    
    [deleteBtn setBackgroundColor:[UIColor colorWithHex:@"40B87B"]];
    [deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    deleteBtn.enabled = YES;
    
    if (!model.isSelected) {
        self.selectedBtn.selected = NO;
        [deleteBtn setBackgroundColor:[UIColor colorWithHex:@"CCCCCC"]];
        [deleteBtn setTitleColor:[UIColor colorWithHex:@"FFFFFF"] forState:UIControlStateDisabled];
        deleteBtn.enabled = NO;
        [_idArray addObject:[NSString stringWithFormat:@"%ld",model.ID]];
    }
    [_waitDiscountTableView reloadData];
    
    if (_idArray.count>0) {
        
        [deleteBtn setBackgroundColor:[UIColor colorWithHex:@"40B87B"]];
        [deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        deleteBtn.enabled = YES;
        
        }else{
        
        [deleteBtn setBackgroundColor:[UIColor colorWithHex:@"CCCCCC"]];
        [deleteBtn setTitleColor:[UIColor colorWithHex:@"FFFFFF"] forState:UIControlStateDisabled];
        deleteBtn.enabled = NO;

    }
    int counts = 0;
    //判断是否全选
    NSIndexPath *indexPath = [_waitDiscountTableView indexPathForCell:cell];
    model = _waitDataArray[indexPath.row];
    for (model in _waitDataArray) {
        if (model.isSelected) {
            counts++;
        }
    }
    if (counts-1 == _waitDataArray.count) {
        self.selectedBtn.selected = YES;
        
        [self.selectedBtn setImage:[UIImage imageNamed:@"icon_checked"] forState:UIControlStateSelected];
        
    }else{
        self.selectedBtn.selected = NO;
    }
    [_waitDiscountTableView reloadData];
}
//计算价格
- (void)numerationPrice{
    
    int orderNu = 0;
    for (int i = 0; i < _waitDataArray.count; i++) {

        SSSWaitModel *model = [_waitDataArray objectOrNilAtIndex:i];
        if (model.isSelected) {
            
            priceNumber = priceNumber + [model.money intValue];
            discountNumber = discountNumber + [model.rangli intValue];
            orderNu++;
        }
    }
    moneyStr = [NSString stringWithFormat:@"%0.2f",priceNumber];
    _allMoneyNuLabel.text = moneyStr;
    _oderNuLabel.text = [NSString stringWithFormat:@"%d",orderNu];
    //NSString *oderNuStr = [NSString stringWithFormat:@"%d",discountNumber];
    //_oderNuLabel.text = oderNuStr;
    
    discountNumber = 0;
    priceNumber = 0.00;
}
- (void)dealloc{
    
     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"sendWaitingNuStr" object:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
}
#pragma mark - 全部选中按钮
- (void)allSelectedBtnClick:(UIButton *)sender{
    
    sender.selected = !sender.selected;
    if (_waitDataArray.count <= 0) {
        showTipHudStr(@"数据暂未加载，暂不能点击");
        self.selectedBtn.enabled = NO;
       // showTipHudStr(@"数据暂未加载，暂不能点击");
    }
    for (int i = 0; i < _waitDataArray.count;i++) {
        
        SSSWaitModel *model = [_waitDataArray objectOrNilAtIndex:i];
        model.isSelected = sender.selected;
        [_idArray removeAllObjects];
        [_idArray addObject:[NSString stringWithFormat:@"%ld",model.ID]];
    }
    
    deleteBtn.enabled = YES;

    if (sender.selected) {

        [deleteBtn setBackgroundColor:[UIColor colorWithHex:@"40B87B"]];
        [deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        deleteBtn.enabled = YES;
        //[_idArray addObject:[NSString stringWithFormat:@"%ld",model.ID]];
        
    }else{
        //遍历模型
        
//        for (int i = 0; i < _waitDataArray.count; i++) {
//            
//            SSSWaitModel *model  = [_waitDataArray objectOrNilAtIndex:i];
//            
//                if (model.isSelected !=sender.selected) {
//                    [_idArray addObject:[NSString stringWithFormat:@"%ld",model.ID]];
//                    [deleteBtn setBackgroundColor:[UIColor colorWithHex:@"CCCCCC"]];
//                    [deleteBtn setTitleColor:[UIColor colorWithHex:@"FFFFFF"] forState:UIControlStateDisabled];
//                    deleteBtn.enabled = NO;
//                    
//                }else{
//                    
//                    [deleteBtn setBackgroundColor:[UIColor colorWithHex:@"40B87B"]];
//                    [deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//                    deleteBtn.enabled = YES;
//                }
//        
//        }
        


        for (int i = 0; i < _waitDataArray.count; i++) {

            SSSWaitModel *model  = [_waitDataArray objectOrNilAtIndex:i];

                if (model.isSelected !=sender.selected) {
                    [_idArray addObject:[NSString stringWithFormat:@"%ld",model.ID]];
                    [deleteBtn setBackgroundColor:[UIColor colorWithHex:@"CCCCCC"]];
                    [deleteBtn setTitleColor:[UIColor colorWithHex:@"FFFFFF"] forState:UIControlStateDisabled];
                    deleteBtn.enabled = NO;

                }else{

                    [deleteBtn setBackgroundColor:[UIColor colorWithHex:@"40B87B"]];
                    [deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                    deleteBtn.enabled = YES;
                }

        }

        [deleteBtn setBackgroundColor:[UIColor colorWithHex:@"CCCCCC"]];
        [deleteBtn setTitleColor:[UIColor colorWithHex:@"FFFFFF"] forState:UIControlStateDisabled];
        deleteBtn.enabled = NO;
    }
    
//    for (int i = 0; i < _waitDataArray.count;i++) {
//        
//        SSSWaitModel *model = [_waitDataArray objectOrNilAtIndex:i];
//        model.isSelected = sender.selected;
//        [_idArray removeAllObjects];
//        [_idArray addObject:[NSString stringWithFormat:@"%ld",model.ID]];
//    }
//
//    deleteBtn.enabled = YES;
    [self numerationPrice];
    [_waitDiscountTableView reloadData];
}
- (void)commitBtn{
    
    SSSOnlinePayViewController *onlineVc = [[SSSOnlinePayViewController alloc]init];
    onlineVc.allMoneyStr = moneyStr;
    onlineVc.idArray = [NSMutableArray arrayWithArray:_idArray];
    [self.navigationController pushViewController:onlineVc animated:YES];
    
}
//- (NSMutableArray *)waitDataArray{
//    
//    if (!_waitDataArray) {
//        _waitDataArray = [NSMutableArray array];
//    }
//    return _waitDataArray;
//}
@end
